package com.jovision.sovplay;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.base.BaseActivity;
import com.aviccxzx.smarthome.entity.StreamFile;

import java.util.ArrayList;


/**
 * V8 流媒体协议下的远程文件列表适配器
 * */
public class CatFileAdapter extends BaseAdapter {

    private ArrayList<StreamFile> streamFileList = new ArrayList<StreamFile>();
    private Context mContext = null;
    private LayoutInflater inflater;

    public CatFileAdapter(Context con) {
        mContext = con;
        inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setData(ArrayList<StreamFile> list) {
        streamFileList = list;
    }

    @Override
    public int getCount() {
        int size = 0;
        if (null != streamFileList && 0 != streamFileList.size()) {
            size = streamFileList.size();
        }
        return size;
    }

    @Override
    public Object getItem(int position) {
        StreamFile rv = new StreamFile();
        if (null != streamFileList && 0 != streamFileList.size()
                && position < streamFileList.size()) {
            rv = streamFileList.get(position);
        }
        return rv;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_file, null);
            viewHolder = new ViewHolder();
            viewHolder.videoDate = (TextView) convertView
                    .findViewById(R.id.videodate);
            viewHolder.videoDisk = (TextView) convertView
                    .findViewById(R.id.videodisk);
            viewHolder.videoDownLoad = (ImageView) convertView
                    .findViewById(R.id.videodownload);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (null != streamFileList && 0 != streamFileList.size()
                && position < streamFileList.size()) {
            viewHolder.videoDate.setText(
                    streamFileList.get(position).getFileDate());

            viewHolder.videoDisk.setText((position+1)+"");
        }

        viewHolder.videoDownLoad.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                ((BaseActivity) mContext).onNotify(JVCatFileListActivity.FLAG_DOWNLOAD_FILE,
                        position, 0, null);
            }
        });

        return convertView;
    }

    class ViewHolder {
        TextView videoDate;
        TextView videoDisk;
        ImageView videoDownLoad;
    }
}
